﻿/*-----------------------------------------------
// File: FirstResourceLoadState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class FirstResourceLoadState : ResourceLoadState 
    {
        BattleViewFirstCtrl firstViewCtrl = null;
        public FirstResourceLoadState(BattleState state, BattleViewFirstCtrl ctrl) : base(state, ctrl) {
            firstViewCtrl = ctrl;
        }

        public enum LoadPhase {
            CheckPhone,
            Load,
            Anim,
        }

        LoadPhase currentPhase;

        public override void Enter()
        {
            sendDeviceData();
        }

        const float checkPhoneTimeout = 1f;
        float checkPhoneDeltaTime = 0;

        void sendDeviceData()
        {
            currentPhase = LoadPhase.CheckPhone;

            ProtoBuf.Message.CSCheckPhone msg = new ProtoBuf.Message.CSCheckPhone();
            msg.infoList.Add(SystemInfo.deviceModel);
            msg.infoList.Add(GameConfig.Instance.GetDeviceID);
            msg.infoList.Add(GameConfig.Instance.AppVersion);
            msg.infoList.Add(GameConfig.Instance.GetChannelId);

            Debug.Log("[FirstResourceLoadState] sendDeviceData in background ....");
            NetAPI.SendHttpInBackground(ProtoBuf.Message.OpDefine.CSCheckPhone, msg, CheckDeciveCallback);
            checkPhoneDeltaTime = 0f;
        }

        void CheckDeciveCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (currentPhase != LoadPhase.CheckPhone)
            {
                Debug.LogWarning("[FirstResourceLoadState] receive deviceData callback timeout...");
                return;
            }
            else
            {
                if (success)
                {
                    ProtoBuf.Message.SCCheckPhone msg = packet.kBody as ProtoBuf.Message.SCCheckPhone;
                    var data = msg.result;
                    //var dic = msg.entry;
                    Debug.Log("[FirstResourceLoadState] receive deviceData callback success :" + data[0] + " " + data[1]);
#if UNITY_EDITOR
                    data[0] = 1;
#endif
                    switch (data[0])
                    {
                        case 1:
                            RenderManager.ShowBloom = false;
                            RenderManager.ShowGlow = false;
                            RenderManager.Quality = RenderLevel.Low;
                            break;
                        case 2:
                            RenderManager.ShowBloom = false;
                            RenderManager.ShowGlow = true;
                            RenderManager.Quality = RenderLevel.Middle;
                            break;
                        case 3:
                            RenderManager.ShowBloom = true;
                            RenderManager.ShowGlow = true;
                            RenderManager.Quality = RenderLevel.High;
                            break;
                    }
                    if (data[1] == 0)
                    {
                        StaticData.isMusicOn = true;
                        StaticData.isSoundOn = true;
                    }
                    else if (data[1] == 1)
                    {
                        StaticData.isMusicOn = false;
                        StaticData.isSoundOn = false;
                    }

                }
                else
                {
                    RenderManager.ShowBloom = false;
                    RenderManager.ShowGlow = true;
                    StaticData.isMusicOn = true;
                    StaticData.isSoundOn = true;
					RenderManager.Quality = RenderLevel.High;
                    Debug.LogError("[FirstResourceLoadState] receive deviceData error! use default setting!");
                }

                EnterLoad();

            }




        }

        void EnterLoad()
        {
            base.Enter();
            currentPhase = LoadPhase.Load;
            Debug.Log("[FirstResourceLoadState] Enter Load Phase....");
        }



        public override void Update()
        {
            if (currentPhase == LoadPhase.CheckPhone)
            {
                checkPhoneDeltaTime += Time.deltaTime;
                if (checkPhoneDeltaTime >= checkPhoneTimeout)
                {
                    Debug.LogWarning("[FirstResourceLoadState] check phone timeout!");
                    EnterLoad();
                }
            }
            else if (currentPhase == LoadPhase.Load)
            {
                if (LoadComplete())
                {
                    ManagerController.Instance.DestoryCGKit();
                    LoadingPanel.Instance.HideLoading();
                    //StartStory3d();
                    ToEntranceState();
                    return;
                }
            }
            else if (currentPhase == LoadPhase.Anim) {

            }
        }

        public override void Leave()
        {
            firstViewCtrl.HideFirstCGPanel();
            firstViewCtrl.curGuideStep = BattleViewFirstCtrl.FirstBattleGuideStep.Begin;
            BattleManager.Instance.battlePanel.guidePanel.Show(null, BattleViewFirstCtrl.FirstBattleGuideStep.Begin);
            base.Leave();
        }

        void StartStory3d() {
            LoadingPanel.Instance.HideLoading();
            currentPhase = LoadPhase.Anim;
            firstViewCtrl.battleCamera.enabled = false;
            firstViewCtrl.storyObj.SetActive(true);
            firstViewCtrl.storyObj.GetComponent<Story3dLogic>().StoryStart();
            firstViewCtrl.storyObj.GetComponent<Story3dLogic>().SetStoryEndCallBack(Story3dEnd);

            FirstCheckPointUtil.CheckPoint(EnterGameCheckPointEnum.PlayEnterGameAnim);
        }

        void Story3dEnd() {
            ResourceLoader.GetInstance().story3dLoader.UnloadStory(firstViewCtrl.storyObj, firstViewCtrl.storyResourceName);
            AssetLoad.ResourceManager.Instance.UnloadUnusedResources();
            firstViewCtrl.battleCamera.enabled = true;
            firstViewCtrl.storyObj.SetActive(false);
            firstViewCtrl.SwitchState(BattleState.EntranceState);
        }

    }
}